module.exports = class extends think.cmswing.rest {
	
   /**
   * 获取分类信息
   * /api/agent 获取全部代理
   * /api/agent/1 获取代理id为1的栏目信息
   * @returns {Promise.<*>}
   */
	
  async getAction() {
   const cmd = this.get('cmd') || '';
   if (think.isEmpty(cmd)){
	   return this.fail('cmd不能为空！');
   } 
   
   if (think.isEmpty(this.id)){
	   return this.fail('代理ID不能为空！');
   }
   
   if(cmd=='isagent'){
	   if (this.id) {
			 const totalid = await this.model('member').where({proxy_id: this.id,status:1}).count('id');
		     if(totalid>0){
		    	return this.success('代理正常！');
		     }else{
		    	 return this.fail('代理信息不存在！');
		     }
       }else{
		    return this.fail('代理ID不能为空！');
	  }
   }else if(cmd=='agentinfo'){
	   let mdata ={};
	   let data = await this.model('member').where({proxy_id:this.id}).field('proxy_id,mobile,coin_count,card_count,desc').find();
	   if(think.isEmpty(data)){
		   return this.fail('代理信息不存在！');
	   }else{
		   mdata.agentId = Number(this.id);
		   mdata.coin = data.coin_count;
		   mdata.card = data.card_count;
		   //"desc":"上下分，请联系：18511112222"
		   mdata.desc = data.desc;
		   console.log('agentinfo data--------:'+JSON.stringify(mdata));
		   return this.success(mdata);
	   }
   }else if(cmd=='recharge'){
	   //type=1&num=1000;
	   const type = this.get('type') || '';
	   const num = this.get('num') || '';
	   const uid = this.get('uid') || '';
	   if (think.isEmpty(type)){
		   return this.fail('充值类型不能为空！');
	   }
	   if (think.isEmpty(num)){
		   return this.fail('充值数量不能为空！');
	   }
	   if (think.isEmpty(uid)){
		   return this.fail('玩家ID不能为空！');
	   }
	   
	   const medata = await this.model('member').where({proxy_id:this.id,status:1,is_admin:0}).find();
       if(think.isEmpty(medata)){
    	   return this.fail('代理信息不存在！');
       }else{
    	   //1代表金币，2代表房卡
    	   if(type==1){
    		  const mires = await this.model('member').where({id:medata.id}).increment('coin_count',num); 
    		  if(think.isEmpty(mires)){
    	    	   return this.fail('充值失败！');
    	      }else{
    	    	//0:账户充值，1:提现,2:订单,3:充卡,4:充币,5:返利,6:奖励金币，7：奖励房卡
     	        	const blog = {
     	                 admin_id: 1,
     	                 user_id: medata.id,
     	                 type:6,
     	                 time: new Date().valueOf(),
     	                 amount: Number(num),
     	                 amount_log: await this.model('member').where({id: medata.id}).getField('coin_count', true),
     	                 note: uid+' 绑定您的代理ID，您获得金币奖励 '+num+' 枚'
     	               };
     	        	const blogres =  await this.model('balance_log').add(blog);
    	    	  return this.success("充值成功！");
    	      }
    	   }else if(type==2){
    		  const mires = await this.model('member').where({id:medata.id}).increment('card_count',num); 
    		  if(think.isEmpty(mires)){
   	    	     return this.fail('充值失败！');
   	          }else{
   	        	  //0:账户充值，1:提现,2:订单,3:充卡,4:充币,5:返利,6:奖励金币，7：奖励房卡
   	        	const blog = {
   	                 admin_id: 1,
   	                 user_id: medata.id,
   	                 type:7,
   	                 time: new Date().valueOf(),
   	                 amount: Number(num),
   	                 amount_log: await this.model('member').where({id: medata.id}).getField('card_count', true),
   	                 note: uid+' 绑定您的代理ID，您获得房卡奖励 '+num+' 张'
   	               };
   	        	const blogres =  await this.model('balance_log').add(blog);
   	        	
   	    	     return this.success("充值成功！");
   	          }
    	   }
       }
   }
  }
  
  async postAction() {
	const data = this.post();
	console.log('member post action-data--------:'+JSON.stringify(data));
    return this.success({name:'post成功！'});
  }
  //数据状态0禁用，1启用，-1删除
  
  async isAgentAction() {
	 
   }
};
